package com.example.zwh.computershop.shopping_cart;

import android.util.Log;

import com.example.zwh.computershop.sqlModel.Cart;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import static android.content.ContentValues.TAG;

public class SQLoperation {

    private static int uid;//用户id
    private static String pid;//商品id
    private static int anum;//商品数量
    private static URL url = null;
    //删除购物车
    public static void deleteCart(final Cart cart) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {

                    uid = cart.getId();//用户id
                    pid = cart.getProductId();//商品id
                    anum = cart.getAddNum();//商品数量

                    /***
                     * 下面方法要用那个，就用哪个其他注释掉
                     */
                    url = new URL("http://39.108.12.42:8080/anServer/dropCartServlet?uid="+uid+"&pid="+pid);

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String str = reader.readLine();
                    Log.d(TAG, "deleteCart 返回结果：(1失败，2成功)"+str);
                } catch (MalformedURLException e) {
                    Log.d(TAG, "错误1");
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    Log.d(TAG, "错误2");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d(TAG, "错误3");
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void modifyCart(final Cart cart) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    uid = cart.getId();//用户id
                    pid = cart.getProductId();//商品id
                    anum = cart.getAddNum();//商品数量

                    /***
                     * 下面方法要用那个，就用哪个其他注释掉
                     */
                    //修改购物车
                    url = new URL("http://39.108.12.42:8080/anServer/alterCartServlet?uid="+uid+"&pid="+pid+"&anum="+anum);

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String str = reader.readLine();
                    Log.d(TAG, "modifyCart 返回结果：(1失败，2成功)"+str);
                } catch (MalformedURLException e) {
                    Log.d(TAG, "错误1");
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    Log.d(TAG, "错误2");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d(TAG, "错误3");
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void setOrder(final myOrder order) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    uid = order.getUserId();//用户id
                    pid = order.getProductId();//商品id
                    anum = order.getNum();//商品数量

                    /***
                     * 下面方法要用那个，就用哪个其他注释掉
                     */
                    url = new URL("http://39.108.12.42:8080/anServer/inOrdServlet?pid="+pid+"&uid="+uid+"&pnum="+anum+"");

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String str = reader.readLine();
                    Log.d(TAG, "modifyCart 返回结果：(1失败，2成功)"+str);
                } catch (MalformedURLException e) {
                    Log.d(TAG, "错误1");
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    Log.d(TAG, "错误2");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d(TAG, "错误3");
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void addCart(final Cart cart) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    uid = cart.getId();//用户id
                    pid = cart.getProductId();//商品id
                    anum = cart.getAddNum();//商品数量

                    /***
                     * 下面方法要用那个，就用哪个其他注释掉
                     */
                    url = new URL("http://39.108.12.42:8080/anServer/inCartServlet?uid="+uid+"&pid="+pid+"&anum="+anum);//加入购物车  不能增加现有的

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String str = reader.readLine();
                    Log.d(TAG, "addCart 返回结果：(1失败，2成功)"+str);
                } catch (MalformedURLException e) {
                    Log.d(TAG, "错误1");
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    Log.d(TAG, "错误2");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d(TAG, "错误3");
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
